<?php
class MemberCenterConfigAction extends CommonAction{

      /*+-------------------------------------
        |       用户邮件格式设置
        +-------------------------------------
       */
       public function memberEmailConfig(){
              C('SHOW_RUN_TIME',false);			// 运行时间显示
              C('SHOW_PAGE_TRACE',false);
              if(!$_POST){
                    //查询系统数据库，用户分类
                    //1.系统管理员
                    $admin   = $this->mailInfo($this->getSysAdmin());
                    $okCheck = $this->mailInfo($this->okCheckWebUser());
                    $noCheck = $this->mailInfo($this->noCheckWebUser());
                    $this->assign("admin",$admin);
                    $this->assign("okCheck",$okCheck);
                    $this->assign("noCheck",$noCheck);
                    $this->display();
              }else{
                  if($_POST['allAdmin']){   //所有系统管理员
                          $this->sendAllAdmin($_POST);
                  }elseif($_POST['allMember']){
                          $this->sendAllMember($_POST);
                  }elseif($_POST['allOkCheck']){
                          $this->sendAllOkCheck($_POST);
                  }elseif($_POST['allNoCheck']){
                          $this->sendAllNoCheck($_POST);
                  }elseif($_POST['send']){
                          $this->send($_POST);
                  }
              }
       }

       //重组mail信息
       private function mailInfo($mailInfo){
               foreach($mailInfo as $key => $val){
                  if($val['sysuser_nikename'] != ''){
                         $info[$key]['userInfo'] = $val['sysuser_nikename']." ".$val['sysuser_email'];
                  }else{
                         $info[$key]['userInfo'] = $val['sysuser_name']." ".$val['sysuser_email']; 
                  }
                  $info[$key]['mail'] = $val['sysuser_email'];
               }
               return $info;
       }

       //系统管理员
       private function getSysAdmin(){
               $user    = D("sysuser");
               $sysuser = $user ->field("id,sysuser_name,sysuser_nikename,sysuser_email")->where("user_type = '1' and sysuser_email != ''") ->select();
               return $sysuser; 
       }

       private function getAllUser(){
               $user    = D("sysuser");
               $sysuser = $user ->field("id,sysuser_name,sysuser_nikename,sysuser_email")->where("sysuser_email != ''") ->select();
               return $sysuser;
       }

       //已审核站点用户
       private function okCheckWebUser(){
               $user    = D("sysuser");
               $sysuser = $user ->field("id,sysuser_name,sysuser_nikename,sysuser_email")->where("user_type = '0' and sysuser_email != '' and status = '1'") ->select();
               return $sysuser;
       }

       //未审核站点用户
       private function noCheckWebUser(){
               $user    = D("sysuser");
               $sysuser = $user ->field("id,sysuser_name,sysuser_nikename,sysuser_email")->where("user_type = '0' and sysuser_email != '' and status = '0'") 
                   ->select();
               return $sysuser;
       }

       private function html2Text($info){
               return htmlspecialchars(stripslashes($info));               
       }
       //处理自定义选择的用户
       private function changeUser($info){
               return $info['email'];
       }

       //发送到所有系统管理员
       private function sendAllAdmin($info){
               //获取所有管理员Email
               $email = $this->getSysAdmin();
               foreach($email as $key => $val){
                      $address[$key] = $val['sysuser_email'];
               }
               $mailInfo['mailTile']    = $info['title'];
               $mailInfo['mailContent'] = $this->html2Text($info['content']);
               $this                    ->sendMail($mailInfo,$address);
               //echo "<pre>";print_r($mailInfo);print_r($address);exit;
       }

       //发送到所有站点用户
       private function sendAllMember($info){
               //获取所有管理员Email
               $email = $this->getAllUser();
               foreach($email as $key => $val){
                      $address[$key] = $val['sysuser_email'];
               }
               $mailInfo['mailTile']    = $info['title'];
               $mailInfo['mailContent'] = $info['content'];
               $this                    ->sendMail($mailInfo,$address);
               //echo "<pre>";print_r($mailInfo);print_r($address);exit;
       }

       //发送到所有已审核站点用户
       private function sendAllOkCheck($info){
               //获取所有管理员Email
               $email = $this->okCheckWebUser();
               foreach($email as $key => $val){
                      $address[$key] = $val['sysuser_email'];
               }
               $mailInfo['mailTile']    = $info['title'];
               $mailInfo['mailContent'] = $info['content'];
               $this                    ->sendMail($mailInfo,$address);
               //echo "<pre>";print_r($mailInfo);print_r($address);exit;
       }

       //发送到所有未审核站点用户
       private function sendAllNoCheck($info){
               //获取所有管理员Email
               $email = $this->getAllUser();
               foreach($email as $key => $val){
                      $address[$key] = $val['sysuser_email'];
               }
               $mailInfo['mailTile']    = $info['title'];
               $mailInfo['mailContent'] = $info['content'];
               $this                    ->sendMail($mailInfo,$address);
               //echo "<pre>";print_r($mailInfo);print_r($address);exit;
       }

       //发送到所有所选择用户
       private function send($info){
               //获取所有管理员Email
               $email = $this->changeUser($info);
               $address= $email;
               $mailInfo['mailTile']    = $info['title'];
               $mailInfo['mailContent'] = $info['content'];
               $this                    ->sendMail($mailInfo,$address);
               //echo "<pre>";print_r($mailInfo);print_r($address);exit;
       }

       /*+------------------------------------
         |     邮件发送
         +------------------------------------*/
       private function sendMail($mailInfo,$address){
               import('@.ORG.class-phpmailer');
               import('@.ORG.class-smtp');

               $mail=new PHPMailer();

               // 设置PHPMailer使用SMTP服务器发送Email
               $mail->ContentType = "text/html";
               $mail->IsSMTP();

               // 设置邮件的字符编码，若不指定，则为'UTF-8'
               $mail->CharSet='utf-8';

               // 添加收件人地址，可以多次使用来添加多个收件人
               foreach($address as $key => $val){
                   $mail->AddAddress($val);
               }

               // 设置邮件正文
               $message= str_replace('\"','"',$mailInfo["mailContent"]);
               $mail->Body=$message;

               // 设置邮件头的From字段。
               // 对于网易的SMTP服务，这部分必须和你的实际账号相同，否则会验证出错。
               $mail->From='myproject_test@sina.com';

               // 设置发件人名字
               $mail->FromName='都好经典设计';

               // 设置邮件标题
               $mail->Subject= $mailInfo["mailTile"];

               // 设置SMTP服务器。这里使用网易的SMTP服务器。
               $mail->Host='smtp.sina.com';

               // 设置为“需要验证”
               $mail->SMTPAuth=true;

               // 设置用户名和密码，即网易邮件的用户名和密码。
               $mail->Username='myproject_test@sina.com';
               $mail->Password='myproject_test';

               // 发送邮件。
               $mail->Send();
               $num = count($address);
               $this->success("成功的发送了 {$num} 个邮件!");
       }

       /*+-------------------------------------
        |       用户文件上传配置
        +-------------------------------------
       */
       public function memberFileUploadConfig(){
              $this->display();
       }

      /*+-------------------------------------
        |      图片水印设置
        +-------------------------------------
       */
       public function imageWatermarkConfig(){
         
              $this->display();
       }
       
}
?>
